package entities
{
	import org.flixel.*;
	import gameStates.*;

	public class Player extends FlxSprite
	{
		[Embed(source = "../../assets/images/player2.png")]public var playerPNG:Class;
		[Embed(source="../../assets/audio/PUSH.mp3")]public var pushSnd:Class;
				
		private var justLand:Boolean = false;
		private var justJump:Boolean = false;
		public var moved:Boolean = false;
		public var pushing:Boolean = false;
		public var playerColor:int = -1;
		//-1 negro,0 rojo,1 amarillo, 2 verde, 3 cyan, 4 algo, 5 azul
		public function Player(_x:int, _y:int,parent:*):void 
		{		
			super(_x, _y);
			loadGraphic(playerPNG,true,false,64,64,true);
			addAnimation("run-1", [9, 10, 11, 12, 13, 14], 10, false);
			addAnimation("jump-1", [5], 1, false);
			addAnimation("startJump-1", [0, 1, 2, 3,4],10 , false);
			addAnimation("landJump-1", [1,2,17,3,8], 10, false);
			addAnimation("idle-1", [15, 16, 17], 2, true);
			addAnimation("push-1", [18, 19, 20, 21, 22, 23], 3, true);
			
			addAnimation("run2", [36,37,38,39,40,41], 10, false);
			addAnimation("jump2", [27,28,29,30,31,32,33,34,35], 5, false);
			addAnimation("idle2", [42, 43, 44], 2, true);
			addAnimation("push2", [45, 46, 47, 48, 49, 50], 3, true);
			
			addAnimation("run1", [63,64,65,66,67,68], 10, false);
			addAnimation("jump1", [54,55,56,57,58,59,60,61,62], 5, false);
			addAnimation("idle1", [69, 70, 71], 2, true);
			addAnimation("push1", [72, 73, 74, 75, 76, 77], 3, true);
			
			addAnimation("run0", [90,91,92,92,93,94], 10, false);
			addAnimation("jump0", [81,82,83,84,85,86,87,88,89], 5, false);
			addAnimation("idle0", [95, 96, 97], 2, true);
			addAnimation("push0", [98, 99, 100, 101, 102, 103], 3, true);
			
			addAnimation("run5", [117,118,119,120,121,122], 10, false);
			addAnimation("jump5", [108,109,110,111,112,113,114,115,116], 5, false);
			addAnimation("idle5", [123,124,125], 2, true);
			addAnimation("push5", [126, 127, 128, 129, 130,131], 3, true);
			
			maxVelocity.x = 200;
			maxVelocity.y = 500;
			acceleration.y = 300;
			drag.x = maxVelocity.x * 2;
			
			addAnimationCallback(JumpAnim);

			play("idle-1");
		}

		private function JumpAnim(name:String, frame:uint, index:uint):void
		{
			if (index == 8)
				justLand = false;
			if (index == 4)
				justJump = false;
		}
		
		public function isPushing(box:FlxSprite, player:FlxSprite):void
		{
			if(y == Registry.box.y)
				pushing = true;
		}
		
		public function changeColor(idCol:int):void
		{
			playerColor = idCol;

		}
		
		override public function update():void
		{
			//Player movement and controls
			acceleration.x = 0;
				
			if (FlxG.keys.LEFT || FlxG.keys.A ) {
				if(scale.x == 1)
					scale.x *= -1;
				this.facing = FlxObject.LEFT;
				acceleration.x = -maxVelocity.x * 2;
				moved = true;
			}
			if (FlxG.keys.RIGHT || FlxG.keys.D) {
				if(scale.x == -1)
					scale.x *= -1;
				this.facing = FlxObject.RIGHT;
				acceleration.x = maxVelocity.x * 2;
				moved = true;
			}
			if (FlxG.keys.justPressed("UP") ||FlxG.keys.justPressed("W") ) {
				if (isTouching(FlxObject.FLOOR)) {
					justJump = true;	
					velocity.y = -maxVelocity.y / 2;
				}
			}
			
			if(justLand)
				play("landJump" + playerColor.toString());
			else if(justJump)
				play("startJump" + playerColor.toString());
			else
			{
				if (moved)
				{
					if (isTouching(FlxObject.FLOOR))
					{	if (pushing){
							play("push" + playerColor.toString());
							FlxG.play(pushSnd,0.1);
					}
						else{
							play("run" + playerColor.toString());
						}
					}
					else{
							play("jump" + playerColor.toString());
					}
				}
				else
				{	if(isTouching(FlxObject.FLOOR)){
						play("idle" + playerColor.toString());
					}
					else {
					
						play("jump" + playerColor.toString());
					}
					
				}
			}
			
			if (this.justTouched(FlxObject.FLOOR))justLand = true;
					
									
			pushing = false;
			moved = false;
			super.update();
		}
		
	}
}
